//
//  NItemSearchView.m
//  News
//
//  Created by zhangtao on 2018/10/7.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "NItemSearchView.h"
#import "ZTButton.h"
#import "UIButton+NBtnBadge.h"
@implementation NItemSearchView
{
    UIButton *selectBtn;
    UIView *bottomView;
}
-(instancetype)initWithFrame:(CGRect)frame items:(NSArray *)items{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        CGFloat itemW = frame.size.height-15;
        _rightBtn = [[ZTButton alloc]initWithFrame:CGRectMake(frame.size.width-itemW-8, 5, itemW, itemW)];
        [_rightBtn setTag:-1];
        [_rightBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [_rightBtn setImage:[UIImage imageNamed:@"xr"] forState:UIControlStateNormal];
        [self addSubview:_rightBtn];
        
        _rightLeftBtn = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMinX(_rightBtn.frame)-itemW-3, 7, itemW-4, itemW-4)];
        [_rightLeftBtn setTag:-2];
        [_rightLeftBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [_rightLeftBtn setImage:[UIImage imageNamed:@"fdj"] forState:UIControlStateNormal];
        [self addSubview:_rightLeftBtn];
        
        if (items) {
            _buttons = [NSMutableArray arrayWithCapacity:0];
            CGFloat leftWeight = 0;
            for (int i = 0; i<items.count; i++) {
                CGSize size = [ACEPublicTool textViewWeightWithTextFont:[UIFont systemFontOfSize:17] andText:items[i] andSize:CGSizeMake(200, itemW)];
                
                UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(leftWeight, 7, size.width+5, itemW)];
                [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                [btn setTitle:items[i] forState:UIControlStateNormal];
                btn.titleLabel.font = [UIFont systemFontOfSize:17];
                [self addSubview:btn];
                
                [btn setTag:i];
                [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
                [_buttons addObject:btn];
                
                leftWeight= size.width+5 + leftWeight + 10;
                
                if (i == 0) {
                    btn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
                    selectBtn = btn;

                    bottomView = [[UIView alloc]initWithFrame:CGRectMake(btn.frame.origin.x+3, frame.size.height-9, btn.frame.size.width-6, 2)];
                    bottomView.backgroundColor = MAIN_COLOR;
                    [self addSubview:bottomView];
                }
            }
        }
//        _rightBtn.badgeNum = 5;
        [_buttons[1] badgeNum:5 andFont:10];
    }
    return self;
}

-(void)click:(UIButton *)btn{
    if (self.ItemSearchViewCallBack) {
        self.ItemSearchViewCallBack(btn.tag);
    }

    if (btn.tag<0) {
        return;
    }
    
    btn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    bottomView.frame = CGRectMake(btn.frame.origin.x+3, bottomView.frame.origin.y, btn.frame.size.width-6, 2);
    selectBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    selectBtn = nil;
    selectBtn = btn;
}

-(void)markBadgeIndex:(NSInteger)index andNum:(NSInteger)num{
    if (_buttons.count>=index) {
        CGSize size = [ACEPublicTool textViewWeightWithTextFont:SUPER_MIN_FONT andText:[NSString stringWithFormat:@"%ld",num] andSize:CGSizeMake(100, 10)];
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(_buttons[index].frame.size.width-5, 0, size.width+4, 10)];
        lab.textAlignment = NSTextAlignmentCenter;
        lab.backgroundColor = [UIColor redColor];
        lab.textColor = [UIColor whiteColor];
        lab.text = [NSString stringWithFormat:@"%ld",num];
        lab.font = [UIFont systemFontOfSize:10];
        lab.layer.cornerRadius = 5;
        lab.layer.masksToBounds = YES;
        [_buttons[index] addSubview:lab];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
